/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Delegate.UserInfoDelegate;
import com.webaccess.retailws.Util.CheckoutUtil;
import com.webaccess.retailws.Util.CommonUtil;
import com.webaccess.retailws.Util.UserAccountUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author Windows
 */
public class CheckoutDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    ActionMessages errors = null;
    UserInfoDelegate userDelegate = null;
    CheckoutUtil chkoutUtil = null;
    UserAccountUtil userAccUtil = null;
    CommonUtil commUtil = null;

    public ActionForward checkoutUserAddress(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        errors = new ActionErrors();
        userDelegate = new UserInfoDelegate();
        chkoutUtil = new CheckoutUtil();
        userAccUtil = new UserAccountUtil();
        commUtil = new CommonUtil();
        HttpSession loginSession = request.getSession(true);
        int editPrefixId = 0, editStateId = 0;
        String editPrefix = "", editFirstName = "", editLastName = "", editMiddleName = "", editHouse = "", editStreet = "", editCity = "", editState = "", editContact = "", editPinCode = "";
        Long userId = 0L, addressId = 0L;

        try {
            commUtil.getTopRightListMap(request);

            userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            addressId = Long.parseLong(loginSession.getAttribute("addressId").toString());
            editPrefixId = Integer.parseInt(request.getParameter("addrTitleId"));
            editPrefix = userDelegate.getPrefix(editPrefixId);
            editFirstName = String.valueOf(request.getParameter("addrFirstName")).trim();
            editMiddleName = String.valueOf(request.getParameter("addrMiddleName")).trim();
            editLastName = String.valueOf(request.getParameter("addrLastName")).trim();
            editHouse = String.valueOf(request.getParameter("house")).trim();
            editStreet = String.valueOf(request.getParameter("street")).trim();
            editCity = String.valueOf(request.getParameter("city")).trim();
            editStateId = Integer.parseInt(request.getParameter("stateId"));
            editState = userDelegate.getState(editStateId);
            editPinCode = String.valueOf(request.getParameter("pinCode")).trim();
            editContact = String.valueOf(request.getParameter("contact")).trim();

            Pattern numericExp = Pattern.compile("/^[0-9]+$/");
            Pattern alphaExp = Pattern.compile("/^[a-zA-Z]+$/");

            Matcher matchFirstName = alphaExp.matcher(editFirstName);
            Matcher matchMiddleName = alphaExp.matcher(editMiddleName);
            Matcher matchLastName = alphaExp.matcher(editLastName);
            Matcher matchCity = alphaExp.matcher(editCity);
            Matcher matchPinCode = numericExp.matcher(editPinCode.toString());
            Matcher matchContact = numericExp.matcher(editContact.toString());

            if (editPrefixId == 0 || editFirstName.equalsIgnoreCase("") || editLastName.equalsIgnoreCase("")) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.editaddress.required"));
            } else if (matchFirstName.find()) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.fname.invalid"));
            } else if (matchMiddleName.find()) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.mname.invalid"));
            } else if (matchLastName.find()) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.lname.invalid"));
            } else if (editHouse.equalsIgnoreCase("") || editStreet.equalsIgnoreCase("") || editCity.equalsIgnoreCase("") || editPinCode.equalsIgnoreCase("") || editStateId == 0 || editContact.equalsIgnoreCase("")) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.editaddress.required"));
            } else if (matchCity.find()) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.city.invalid"));
            } else if (matchPinCode.find()) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.pincode.invalid"));
            } else if (matchContact.find()) {
                errors.add("errCheckoutDelAddr", new ActionMessage("errors.contact.invalid"));
            } else {
                SUCCESS = chkoutUtil.saveAddressInfo(userId, addressId, editPrefix, editFirstName, editMiddleName, editLastName, editHouse, editStreet, editCity, editPinCode, editState, editContact);
            }
            saveErrors(request, errors);
            if (errors.isEmpty()) {
                SUCCESS = "checkoutaddrsuccess";
            } else {
                SUCCESS = "checkoutaddrfailure";
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    
}
